<?php
	include_once 'wxpay/lib/WxPay.Api.php';
	//require_once "wxpay/lib/WxPay.Exception.php";

	class wxpay extends common{
		private $wxpay_config;
		
		function __construct(){
			parent::__construct();
		}	
			
		/**
		 * 生成代签名数据和签名
		 * @param string $out_trade_no
		 * @return unknown[]|string[]
		 */
		function createSign($param){
			extract($param);
			$WxPayApic = new WxPayApi();
			
			$out_trade_no = date('Ymd').rand(1000,9999);
			$order_id = $param['order_id'];
			$table = 'company_order';
			
			//计算总金额
			$sql = $table . ' WHERE id = "' . $order_id . '"';
			$order = $this->getInfo($sql);

			$data = array(
				'appid'=> WxPayConfig::APPID,								//应用ID
				'mch_id'=>WxPayConfig::MCHID,							//商户号
				'device_info'=>'',									//设备号
				'nonce_str'=>$WxPayApic->getNonceStr(),					//随机字符串
				'body'=>'订单支付',								//商品描述
				'detail'=>'',										//商品详情
				'attach'=> $order_id,								//附加数据
				'out_trade_no'=>$out_trade_no,						//商户订单号
				'fee_type'=>'CNY',									//货币类型
				'total_fee'=>$order['pay_money'] * 100,							//总金额
				'spbill_create_ip'=>$spbill_create_ip,				//终端IP
				'time_start'=>'',									//交易起始时间
				'time_expire'=>'',									//交易结束时间
				'goods_tag'=>'',									//商品标记
				'notify_url'=>'http://115.28.175.114/company/wxpay_notify.php',	//通知地址
				'trade_type'=>'APP',								//交易类型
				'limit_pay'=>'',									//指定支付方式
			);
			$WxPayDataBasec = new WxPayDataBase($data);
			$data['sign'] = $WxPayDataBasec->MakeSign();

			$xml = $WxPayDataBasec->ToXml();
			
			$xxl = $WxPayApic->postXmlCurl($xml, 'https://api.mch.weixin.qq.com/pay/unifiedorder');
			
			$res = $WxPayDataBasec->FromXml($xxl);
			
			$res = $this->createSign2($res['prepay_id']);
			
			return $res;
		}

		function createSign2($prepayid){
			$WxPayApic = new WxPayApi();
			$data = array(
					'appid'=> WxPayConfig::APPID,
					'partnerid'=>WxPayConfig::MCHID,
					'prepayid'=>$prepayid,
					'package'=>'Sign=WXPay',
					'noncestr'=>$WxPayApic->getNonceStr(),
					'timestamp'=>time(),
			);
			$WxPayDataBasec = new WxPayDataBase($data);
			$data['sign'] = $WxPayDataBasec->MakeSign();
			return $data;
		}
		
		function success($transaction_id , $order_id , $type = 'payed'){
			//订单状态
			$table = 'company_order';
			$field = array('wxpay_code' => $transaction_id , 'pay_status' => '2');
			if($type == 'finished'){
				$field = array('wxpay_code' => $transaction_id , 'pay_status' => '5' , 'trading_status' => '2');
			}elseif($type == 'refund'){
				$field = array('pay_status' => '4' , 'trading_status' => '2');
			}
			$field['pay_style'] = '1';
			$this->update($table, $field , 'id = "'.$order_id.'"');
		}

		
		/**
		 * 买家退款申请
		 * @param  [type] $param 
		 * @return [type]        
		 */
		function refundApply($param){
			extract($param);
			$order_table = 'company_order';
			$order = $this->getInfo($order_table . ' WHERE id ="'.$order_id.'"');
			if($order['id']){
				$arr = array(
					'order_id' => $order_id,
					'type' => 1,
					'refund_cash' => $refund_cash,
					'reason' => $reason
					);
				$nid = $this->insert('company_refund' , $arr);
				if($nid) {
					$error = 0;
					$msg = '';
					$res = $this->update($order_table ,array('trading_status' => '4') , 'id = "'.$order_id.'"');
				}else{
					$error = 1;
					$msg = '申请失败';
				}
			}else{	
				$error = 1;
				$msg = '订单不存在';
			}
			return array('error' => $error , 'msg' => $msg);
		}

	}
?>